var co = require('co');


function action1(success, fail) {
	setTimeout(() => {
		success()
	}, 1000)
}

function action2(success, fail) {
	setTimeout(() => {
		fail("act2 err")
	}, 1000)
}

action1_p = new Promise((resolve, reject) => {
	action1(() => {
		resolve("act1")
	}, () => {
		reject()
	})
})

action2_p = new Promise((resolve, reject) => {
	action2(() => {
		resolve("act2")
	}, () => {
		reject()
	})
})

co(function *() {
  // resolve multiple promises in parallel
  let a = yield action1_p
  let b = yield action2_p
  var res = yield [a, b];
  console.log(res);
  // => [1, 2, 3]
}).catch(onerror);

// errors can be try/catched
co(function *(){
  try {
    yield Promise.reject(new Error('boom'));
  } catch (err) {
    console.error(err.message); // "boom"
 }
}).catch(onerror);

function onerror(err) {
  // log any uncaught errors
  // co will not throw any errors you do not handle!!!
  // HANDLE ALL YOUR ERRORS!!!
  console.error(err.stack);
}
